/************************************************************************/
/* Some codeparts are from the ASSIMP OpenGL sample                     */
/* Copyright (c) 2008-2010, ASSIMP Development Team                     */
/* http://assimp.sourceforge.net/index.html                             */
/************************************************************************/

#ifndef MODEL_H
#define MODEL_H

#include <hash_map>
#include <string>

#include <NxPhysics.h>
#include <assimp/assimp.h>
#include <assimp/aiPostProcess.h> 

#include "Modelpart.h"
#include "ModelBoundingVolume.h"
#include "ModelParamAiInfo.h"
#include "../Log.h"
#include "../DevilTexture.h"


class Model
{
public:
    Model();
    Model(Log *log);
    virtual ~Model();

    inline void SetLog(Log *log) { this->log = log; }

    int GetNbModelparts();
    Modelpart **GetModelparts();

    const std::string &GetModelFilePath();
    const std::string &GetModelFileExtension();

    int Load(const char *filepath, float scalefactor, NxPhysicsSDK *sdk, ModelBoundingVolume::SHAPETYPE shapetype);
    int LoadModelOnly(const char *filepath, float scalefactor);

    void Draw();

private:
    const aiScene *scene;
    Log *log;
    std::string modelFilepath;
    std::string fileExtension;
    stdext::hash_map<std::string, DevilTexture*> textures;
    int nbModelparts;
    Modelpart **modelparts;

    bool isLoaded;
    float scalefactor;
    unsigned int completeModelDisplayList;

    void Color4ToFloat4(const aiColor4D *c, float f[4]);
    void SetFloat4(float f[4], float a, float b, float c, float d);
    void Color4f(const aiColor4D *color);
    void ApplyMaterial(const aiMaterial *mtl);

    void TraverseNodes(const aiScene *scene, const aiNode* node, Modelpart **modelpartsPointerArray, int &arrayIndex, unsigned int &displayListIndex, NxPhysicsSDK *sdk, ModelBoundingVolume::SHAPETYPE shapetype, aiMatrix4x4 parentTransformation);
    unsigned int CreateDisplayListForMesh(const ModelAiParamInfo &modelAiInfo, unsigned int &displayListIndex);
    void GenerateDisplayLists(NxPhysicsSDK *sdk, ModelBoundingVolume::SHAPETYPE shapetype);

};

#endif //MODEL_H
